<template>
  <div id="login">
    <div class="rootMain">
      <div class="login">
        <div class="leftTxt" @click="backBtn">
          <van-icon name="arrow-left" />
        </div>
        <div class="rightTxt" @click="$router.go(-1)" v-show="false">返回</div>
        <div class="topImg">
          <div>
            <img src="@/assets/img/fuze/icon200.png" alt />
          </div>
        </div>
        <div class="con">
          <div class="form">
            <!-- <div style="width:0;height:0;overflow:hidden;opacity:0;margin:0;padding:0;">
              <input type="text" />
              <input type="password" />
            </div>-->
            <div>
              <span class="iconfont icon-yaoqinghaoyou"></span>
              <input
                type="text"
                v-model="account"
                :placeholder="$t('m.accountP')"
                @keyup.enter="login"
              />
            </div>
            <div>
              <span class="iconfont icon-mima"></span>
              <input
                type="text"
                v-if="showPwd"
                v-model="pwd"
                :placeholder="$t('m.pwdP')"
                @keyup.enter="login"
              />
              <input
                type="password"
                v-else
                v-model="pwd"              
                :placeholder="$t('m.pwdP')"
                @keyup.enter="login"
              />
              <label :class="[showPwd?'show':'']" @click="showPwd=!showPwd"></label>
            </div>
            <p class="forget">
              <b @click="forget">{{$t('m.forgotAccount')}}?</b>
            </p>
          </div>
          <div class="btnBox">
            <div @click="login">{{$t('m.login')}}</div>
          </div>
        </div>
      </div>
    </div>
    <userAgreement v-if="showuserAgreement" @closeuserAgreement="closeuserAgreement"></userAgreement>
    <RootLoading v-show="rootLoading"></RootLoading>
  </div>
</template>
<script>
import Services from "@/service/index.js";
import Header from "@/components/header";
import Footer from "@/components/footer";
import userAgreement from "@/components/userAgreement";
import { Dialog, Toast } from "vant";
import RootLoading from "@/components/rootLoading";

export default {
  data() {
    return {
      account: "",
      pwd: "",
      showPwd: false,
      read: true,
      showuserAgreement: false,
      rootLoading: false
    };
  },
  created() {},
  components: { userAgreement, RootLoading },
  computed: {},
  methods: {
    closeuserAgreement(msg) {
      this.showuserAgreement = false;
    },
    backBtn() {
      if (sessionStorage.getItem("loginBack")) {
        sessionStorage.removeItem("loginBack");
        this.$router.go(-2);
      } else {
        this.$router.go(-1);
      }
    },
    login() {
      // 数字
      const regex_num = /^[0-9]*$/;
      const regex_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
      // console.log(regex_num.test(this.account));
      //    console.log(regex_email.test(this.account));
      if (!this.account) {
        Toast(this.$t("m.accountP"));
        return false;
      }
      if (regex_num.test(this.account)) {
        //      console.log("纯数字");
        if (this.account.length != 11) {
          Toast(this.$t("m.accountP1"));
          // Toast("手机");
          return false;
        }
      } else {
        //      console.log("包含其他");
        if (!regex_email.test(this.account)) {
          Toast(this.$t("m.accountP1"));
          // Toast("邮箱");
          return false;
        }
      }
      if (!this.pwd) {
        Toast(this.$t("m.pwdP"));
        return false;
      }
      if (!this.read) {
        Toast(this.$t("m.rulerP1"));
        return false;
      }
      this.rootLoading = true;
      Services.login({
        mobile: this.account,
        password: this.pwd,
        lang: localStorage.getItem("language")
      }).then(res => {
        if (res.code == "0000") {
          localStorage.setItem("token", res.token);
          localStorage.setItem("userObj", JSON.stringify(res.user));
          this.rootLoading = false;
          if (this.$route.query.music == 1) {
            this.$router.push(`/music`);
            return;
          }
          if (sessionStorage.getItem("speed")) {
            this.$router.push(`/speed`);
            sessionStorage.removeItem("speed");
            return;
          }
          this.$router.push(`/home`);
        } else {
          this.rootLoading = false;
          Toast(res.msg);
        }
      });
    },
    //忘记密码
    forget() {
      this.$router.push(`/forget?account=${this.account}`);
    }
  }
};
</script>
<style lang="scss" scoped type="text/scss">
#login {
  height: 100%;
  .rootMain {
    overflow-y: auto;
    height: 100%;
  }
  .login {
    height: 100%;
    position: relative;
    .topImg {
      > div {
        border-radius: 50%;
        overflow: hidden;
        margin-bottom: 0.8rem;
      }
    }
    .leftTxt {
      position: absolute;
      left: 0.3rem;
      top: 0.3rem;
      color: #40404c;
      i {
        font-size: 0.32rem;
      }
    }
    .rightTxt {
      position: absolute;
      right: 0.3rem;
      top: 0.3rem;
      color: #40404c;
    }
    h1 {
      margin-top: 0.2rem;
      font-size: 0.4rem;
      font-weight: bold;
      letter-spacing: 0.05rem;
      text-align: center;
      color: #fff;
    }
    .con {
      background: #fff;
      min-height: calc(100% - 4.4rem);
    }
    .form {
      padding: 0.35rem 0.5rem;
      p {
        color: #40404c;
        font-size: 0.24rem;
        padding: 0.2rem 0;
      }
      > div {
        background: linear-gradient(to right, #f8f8f8, #d3d3d5);
        border-radius: 0.2rem;
        height: 0.75rem;
        display: flex;
        align-items: center;
        padding: 0 1rem;
        position: relative;
        margin-bottom: 0.3rem;
        overflow: hidden;

        span {
          position: absolute;
          top: 0;
          left: 0;
          width: 1rem;
          height: 100%;
          text-align: center;
          line-height: 0.75rem;
          font-size: 0.3rem;
          color: #e4b577;
        }
        &:last-child {
          margin-bottom: 0;
          padding-right: 1.5rem;
        }
        &:nth-of-type(1) {
          span {
            font-size: 0.34rem;
          }
        }

        input {
          flex: 1;
          border: none;
          outline: none;
          height: 100%;
          background: transparent;
          padding: 0 0.2rem;
          color: #40404c;
          font-size: 0.26rem;
          &::-webkit-input-placeholder {
            color: #999;
          }
        }
        label {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: 0.3rem;
          width: 0.4rem;
          height: 0.4rem;
          background-repeat: no-repeat;
          background-position: center;
          background-size: 100%;
          background-image: url("~@/assets/img/common/buyan@2x.png");
          &.show {
            background-image: url("~@/assets/img/common/zhengyan@2x.png");
          }
        }
        div {
          position: absolute;
          right: 0;
          top: 0;
          width: 1.75rem;
          height: 100%;
          line-height: 0.75rem;
          text-align: center;
          color: #40404c;
          border-left: 1px solid #4e4e4e;
          background: #333541;
          &.disabled {
            color: #ddd;
          }
        }
      }
      .tips {
        color: #c0c1c4;
        padding: 0 0.3rem 0.2rem;
        font-size: 0.24rem;
      }
    }
    .btnBox {
      padding: 0.5rem;
      div {
        border-radius: 0.2rem;
        height: 0.75rem;
        line-height: 0.75rem;
        text-align: center;
        background: linear-gradient(to right, #43434e, #727181);
        color: #fff;
        font-size: 0.32rem;
        &.disabled {
          opacity: 0.4;
        }
      }
    }
    .ruler {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 0.2rem;
      b {
        position: relative;
        width: 14px;
        height: 14px;
        background: #333541;
        transform: rotate(45deg);
        border: 1px solid #333541;
        border-radius: 2px;
        &.checked::before {
          content: "";
          display: block;
          width: 8px;
          height: 8px;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translateX(-50%) translateY(-50%);
          background: #fceba8;
          border-radius: 2px;
        }
      }
      p {
        max-width: 80%;
        margin-left: 0.15rem;
        color: #c0c1c4;
        label {
          color: #fceba8;
        }
      }
    }
  }
}
</style>